package com.supms.course.service.impl;

import java.util.List;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanValidators;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.supms.course.mapper.TeacherMapper;
import com.supms.course.domain.Teacher;
import com.supms.course.service.ITeacherService;

import javax.validation.Validator;

/**
 * 职工管理Service业务层处理
 * 
 * @author LiangHao
 * @date 2022-02-24
 */
@Service
public class TeacherServiceImpl implements ITeacherService 
{

    private static final Logger log = LoggerFactory.getLogger(TeacherServiceImpl.class);

    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询职工管理
     * 
     * @param tId 职工管理主键
     * @return 职工管理
     */
    @Override
    public Teacher selectTeacherByTId(Long tId)
    {
        return teacherMapper.selectTeacherByTId(tId);
    }

    /**
     * 查询职工管理列表
     * 
     * @param teacher 职工管理
     * @return 职工管理
     */
    @Override
    public List<Teacher> selectTeacherList(Teacher teacher)
    {
        return teacherMapper.selectTeacherList(teacher);
    }

    /**
     * 新增职工管理
     * 
     * @param teacher 职工管理
     * @return 结果
     */
    @Override
    public int insertTeacher(Teacher teacher)
    {
        // 先将最后一名职工的工号查询出来，+1后再进行新增操作
        String tNumOld = teacherMapper.getTNumByDescLimit1();

        if (tNumOld != null && tNumOld.length() > 0) {
            int num = Integer.parseInt(tNumOld) + 1;
            String tNumNew = String.valueOf(num);

            teacher.settNum(tNumNew);
            String operName = SecurityUtils.getUsername();
            teacher.setCreateBy(operName);
            teacher.setCreateTime(DateUtils.getNowDate());

            return teacherMapper.insertTeacher(teacher);
        } else return 0;
    }

    /**
     * 修改职工管理
     * 
     * @param teacher 职工管理
     * @return 结果
     */
    @Override
    public int updateTeacher(Teacher teacher)
    {
        String operName = SecurityUtils.getUsername();
        teacher.setUpdateBy(operName);
        teacher.setUpdateTime(DateUtils.getNowDate());
        return teacherMapper.updateTeacher(teacher);
    }

    /**
     * 批量删除职工管理
     * 
     * @param tIds 需要删除的职工管理主键
     * @return 结果
     */
    @Override
    public int deleteTeacherByTIds(Long[] tIds)
    {
        return teacherMapper.deleteTeacherByTIds(tIds);
    }

    /**
     * 删除职工管理信息
     * 
     * @param tId 职工管理主键
     * @return 结果
     */
    @Override
    public int deleteTeacherByTId(Long tId)
    {
        return teacherMapper.deleteTeacherByTId(tId);
    }

    /**
     * 导入职工数据
     *
     * @param teacherList 用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importUser(List<Teacher> teacherList, boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(teacherList) || teacherList.size() == 0)
        {
            throw new ServiceException("导入职工数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Teacher teacher : teacherList)
        {
            try
            {
                // 验证是否存在职工号相同或职工已存在
                Teacher t = teacherMapper.selectTeacherByTNum(teacher.gettNum());
                if (StringUtils.isNull(t))
                {
                    BeanValidators.validateWithException(validator, teacher);
                    teacher.setCreateBy(operName);
                    this.insertTeacher(teacher);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、教职工 " + teacher.gettName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, teacher);
                    teacher.setUpdateBy(operName);
                    this.updateTeacher(teacher);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、教职工 " + teacher.gettName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、教职工 " + teacher.gettName() + " 已存在/职工号重复");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、教职工 " + teacher.gettName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

}
